package com.aorise.controller.bayonet;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.SystemException;
import com.aorise.model.bayonet.*;
import com.aorise.service.bayonet.BayonetService;
import com.aorise.utils.*;
import com.aorise.utils.json.InnerResult;
import com.aorise.utils.json.JsonResponseData;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/19
 * @Description:    卡口模块控制器
 */
@Slf4j
@RestController
@Api(description ="卡口模块控制器")
@RequestMapping("api/bayonet")
public class BayonetController {

    @Autowired
    BayonetService bayonetService;


    /**
     * 添加卡口
     * HTTP 方式：POST
     * API 路径：/api/bayonet/addBayonet
     * 方法名：addBayonet
     * 方法返回类型：String
     */
    @RequiresPermissions("KKGLBJ")
    @ApiOperation(value = "添加卡口", httpMethod = "POST", response = String.class, notes = "添加卡口,提供卡口添加使用")
    @RequestMapping(value = "/addBayonet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addBayonet(@RequestBody @Validated BayonetModel bayonetModel,BindingResult result) {

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            if(bayonetService.addBayonet(bayonetModel)) {
                return JsonResponseData.ofSuccess(null);
            }
            return JsonResponseData.ofFailure();
        }catch (SystemException e) {
            log.error(
                    "controller:BayonetController. function:addBayonet...msg: addBayonet SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataValidationException e) {
            log.error(
                    "controller:BayonetController. function:addBayonet...msg: addBayonet DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:addBayonet...msg:addBayonet DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:addBayonet...msg:addBayonet Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 卡口分页列表
     * HTTP 方式：GET
     * API 路径：/api/bayonet/getBayonetByPage
     * 方法名：getBayonetByPage
     * 方法返回类型：String
     */
    @RequiresPermissions("KKGLFW")
    @ApiOperation(value = "卡口分页列表", httpMethod = "GET", response = String.class, notes = "卡口分页列表,提供卡口添加使用")
    @RequestMapping(value = "/getBayonetByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetByPage(BayonetPageVO bayonetPageVO) {
        Page bayonetPage =null;
        try {
            bayonetPage =bayonetService.getBayonetByPage(bayonetPageVO);
        }catch (DataValidationException e) {
            log.error(
                    "controller:BayonetController. function:getBayonetByPage...msg: getBayonetByPage DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:getBayonetByPage...msg:getBayonetByPage DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:BayonetController. function:getBayonetByPage...msg:getBayonetByPage Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(bayonetPage);
    }

    /**
     * 下载导入卡口模板
     * HTTP 方式：GET
     * API 路径：/api/bayonet/download
     * 方法名：downloadBayonetTemplate
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "下载导入卡口模板", httpMethod = "GET", response = String.class, notes = "下载导入卡口模板，提供卡口下载导出卡口信息模版使用")
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public String downloadBayonetTemplate(HttpServletResponse response) {
        log.debug("Request RESTful API:downloadBayonetTemplate");
        String templateName ="bayonet";
        try {
            bayonetService.downloadBayonetTemplate(response,templateName);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:downloadBayonetTemplate...msg:downloadBayonetTemplate Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        }
    }

    /**
     * 导出卡口信息
     * HTTP 方式：GET
     * API 路径：/api/bayonet/exportBayonet
     * 方法名：exportBayonet
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKGLFW")
    @ApiOperation(value = "导出卡口信息", httpMethod = "GET", response = String.class, notes = "导出卡口信息，提供卡口导出卡口信息使用")
    @RequestMapping(value = "/exportBayonet", method = RequestMethod.GET)
    public String exportBayonet(BayonetPageVO bayonetPageVO,HttpServletResponse response) {
        log.debug("Request RESTful API:exportBayonet");
        log.debug("exportBayonet {}",bayonetPageVO);
        try {
            bayonetService.exportBayonet(bayonetPageVO,response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:exportBayonet...msg:exportBayonet DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:exportBayonet...msg:exportBayonet Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 导入卡口信息
     * HTTP 方式：POST
     * API 路径：/api/bayonet/importBayonet
     * 方法名：importBayonet
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKGLBJ")
    @ApiOperation(value = "导入卡口信息", httpMethod = "POST", response = String.class, notes = "导入卡口信息，提供卡口导入卡口信息使用")
    @RequestMapping(value = "/importBayonet", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String importBayonet(MultipartFile excelFile) {
        log.debug("Request RESTful API:importBayonet");
        try {
            InnerResult<String> result = bayonetService.importBayonet(excelFile);
            String msg = result.getObject().toString();
            if (result.getSuccess()) {
                return new JsonResponseData(true, msg, StatusDefine.SUCCESS, msg, null).toString();
            } else {
                return new JsonResponseData(false, msg, StatusDefine.DATA_FORMAT_ERROR, msg,
                        null).toString();
            }
        } catch (DataValidationException e) {
            log.error(
                    "controller:BayonetController. function:importBayonet...msg:importBayonet DataValidationException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:importBayonet...msg:importBayonet DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:importBayonet...msg:importBayonet Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 卡口详情
     * HTTP 方式：GET
     * API 路径：/api/bayonet/getBayonetInfo
     * 方法名：getBayonetInfo
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKGLFW")
    @ApiOperation(value = "卡口详情", httpMethod = "GET", response = String.class, notes = "卡口详情，提供卡口查看编辑信息使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bayonetID", value = "卡口ID",  paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getBayonetInfo", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetInfo(Integer bayonetID) {
        log.debug("Request RESTful API:getBayonetInfo");
        log.debug("bayonetID {}",bayonetID);
        try {
            BayonetInfo bayonetInfo = bayonetService.getBayonetInfo(bayonetID);
            if(bayonetInfo !=null){
                return JsonResponseData.ofSuccess(bayonetInfo);
            }
            return JsonResponseData.ofFailure();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:getBayonetInfo...msg:getBayonetInfo DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:getBayonetInfo...msg:getBayonetInfo Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 修改卡口信息
     * HTTP 方式：PUT
     * API 路径：/api/bayonet/upBayonet
     * 方法名：upBayonet
     * 方法返回类型：String
     *
     */
    @RequiresPermissions("KKGLBJ")
    @ApiOperation(value = "修改卡口信息", httpMethod = "PUT", response = String.class, notes = "修改卡口信息，提供卡口修改信息使用")
    @RequestMapping(value = "/upBayonet", method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String upBayonet(@RequestBody @Validated BayonetModel bayonet,BindingResult result) {
        log.debug("Request RESTful API:upBayonet");
        log.debug("bayonetModel {}",bayonet);
        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            if(bayonetService.upBayonet(bayonet)){
                return JsonResponseData.ofSuccess(null);
            }
            return JsonResponseData.ofFailure();
        }catch (SystemException e) {
            log.error(
                    "controller:BayonetController. function:upBayonet...msg: upBayonet SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataValidationException e) {
            log.error(
                    "controller:BayonetController. function:upBayonet...msg: upBayonet DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:upBayonet...msg:upBayonet DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:upBayonet...msg:upBayonet Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 获取卡口分组情况
     * HTTP 方式：GET
     * API 路径：/api/bayonet/getBayonetGroup
     * 方法名：getBayonetGroup
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "获取卡口分组情况", httpMethod = "GET", response = String.class, notes = "获取卡口分组情况，提供卡口,卡口设备分组使用")
    @RequestMapping(value = "/getBayonetGroup", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetGroup() {
        log.debug("Request RESTful API:getBayonetGroup");
        try {
            List<BayonetGroup> bayonetGroups = bayonetService.getBayonetGroup();
            return JsonResponseData.ofSuccess(bayonetGroups);
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:getBayonetGroup...msg:getBayonetGroup DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:getBayonetGroup...msg:getBayonetGroup Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 通过分组获取一级分组下的卡口和二级分组下的卡口
     * HTTP 方式：GET
     * API 路径：/api/bayonet/getBayonetByGroup
     * 方法名：getBayonetByGroup
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "通过分组获取一级分组下的卡口和二级分组下的卡口", httpMethod = "GET", response = String.class, notes = "通过分组获取一级分组下的卡口和二级分组下的卡口，提供卡口,卡口设备分组使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组ID,如果不传则查询所有卡口,传则查询一级分组极二级分组下的卡口",  paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getBayonetByGroup", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetByGroup(Integer groupId) {
        log.debug("Request RESTful API:getBayonetByGroup");
        log.debug("groupId {}",groupId);
        try {
            List<BayonetVO> bayonet = bayonetService.getBayonetByGroup(groupId);
            return JsonResponseData.ofSuccess(bayonet);
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:getBayonetByGroup...msg:getBayonetByGroup DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:getBayonetByGroup...msg:getBayonetByGroup Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }
}
